package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.RoundsListAdapter;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.RoundsModel;
import com.jiayang.hhotel.presenter.RoundsPresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.custom.TitleListener;
import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/18 0018.
 * 查房页面
 */

public class RoundsActivity extends XActivity<RoundsPresenter> implements TitleListener, RoundsListAdapter.btClickListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private SwipeRefreshLayout refreshLayout;
    private SwipeMenuRecyclerView recyclerView;//列表控件

    private RoundsListAdapter adapter;

    private int page = 1;

    @Override
    public void initData(Bundle savedInstanceState) {

        initTitle(this,true,getString(R.string.rounds),R.color.orange_color,getString(R.string.history),this);

        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        refreshLayout= (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        recyclerView= (SwipeMenuRecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        isLoadMore = true;
        isFresh(true);
        getP().asyncData(Constant.LAUGUAGE_FLAG_CHINESE,0,page);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //todo 下拉刷新
                if(!isLoadMore){
                    page = 1;
                    getP().asyncData(Constant.LAUGUAGE_FLAG_CHINESE,0,page);
                }else{
                    isFresh(false);
                }
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (!recyclerView.canScrollVertically(1)) {// 手指不能向上滑动了
                    if (!isLoadMore) {
                        isLoadMore = true;
                        page++;
                        getP().asyncData(Constant.LAUGUAGE_FLAG_CHINESE, 0, page);
                    }
                }
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_rounds;
    }

    @Override
    public RoundsPresenter newP() {
        return new RoundsPresenter();
    }

    @Override
    public void rightClickListener() {
        Intent clean=new Intent(this,RoundsHisActivity.class);
        clean.putExtra(RoundsHisActivity.HISTORY_FLAG_CLEAN_OR_ROUNDS, RoundsHisActivity.INTENT_FLAG_ROUNDS);
        startActivity(clean);
    }

    public void isFresh(final boolean is){
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(is);
            }
        });
    }

    public void setAdapter(List<RoundsModel.PCheckRoomListDtosBean> resList){
        isFresh(false);
        if(adapter==null){
            adapter=new RoundsListAdapter(this,resList,true,page,this);
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onBTClickListener(RoundsModel.PCheckRoomListDtosBean model, int id, int position) {
        switch (id){
            case R.id.bt_start://开始
                CommonUtil.showProgressDialog(this,R.string.loading);
                getP().btClick(Constant.LAUGUAGE_FLAG_CHINESE,0,0,0,0,0,0);
                break;
            case R.id.bt_determine://完成查房按钮
                CommonUtil.showProgressDialog(this,R.string.loading);
                getP().btClick(Constant.LAUGUAGE_FLAG_CHINESE,1,reMark1,0,0,0,0);
                break;
            case R.id.bt_thing://客房物品
                Intent rounds=new Intent(this,RoomThingActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("1",model);
                rounds.putExtras(bundle);
                rounds.putExtra(RoomThingActivity.INTENT_ACTIVITY_FLAG, RoomThingActivity.INTENT_FLAG_ROUNDS);
                startActivityForResult(rounds,Constant.GUEST_ROOM_THING_REQUEST_CODE);
                break;
        }
    }

    private boolean isLoadMore;//判断是否正在加载。用于防止加载更多时重复加载
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        LogUtil.showLog("error="+ StringUtil.errMsg(state));
        isFresh(false);
        isLoadMore = false;
        if(page == 1){//只有第一次加载不出数据时才会显示占位图
            refreshLayout.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(R.mipmap.ic_error);
                    tv.setText(getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            refreshLayout.setVisibility(View.VISIBLE);
                            layout.setVisibility(View.GONE);
                            isFresh(true);
                            getP().asyncData(Constant.LAUGUAGE_FLAG_CHINESE,0,page);
                        }
                    });
                    break;
                default://其他错误
                    icon.setImageResource(R.mipmap.ic_launcher);
                    tv.setText(getString(R.string.net_error));
                    icon.setOnClickListener(null);
                    break;
            }
        }else{
            ToastUtil.Toast(StringUtil.errMsg(state),this);
        }
    }

    private int reMark1;//是否使用物品（0-未使用，1-使用）
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==Constant.GUEST_ROOM_THING_REQUEST_CODE&&resultCode==Constant.GUEST_ROOM_THING_RESULT_CODE){
            reMark1 = data.getIntExtra(RoomThingActivity.GUEST_THING_BACK_INTENT_KEY,1);
        }
    }
}
